package com.xyht.sca_s.student_manage_system.modules.rxRegistrationExamine.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.entity.RxRegistrationPlanAppSetting;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.entity.RxRegistrationPlanSetting;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.mapper.RxRegistrationPlanAppSettingMapper;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.mapper.RxRegistrationPlanSettingMapper;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationAdvance.entity.RxRegistrationAdvanceStudentInfo;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationAdvance.mapper.RxRegistrationAdvanceStudentInfoMapper;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationExamine.entity.RxRegistrationExamine;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationExamine.entity.RxRegistrationExamineBlackList;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationExamine.entity.req.RxRegistrationExamineReq;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationExamine.entity.resp.RxRegistrationExamineResp;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationExamine.mapper.RxRegistrationExamineBlackListMapper;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationExamine.mapper.RxRegistrationExamineMapper;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationExamine.service.RxRegistrationExamineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.Date;
import java.util.List;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.rxRegistration.constant.PlanStatusConstant.PLAN_STATUS_ENABLE;

/**
 * <p>
 * 审核应用 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-05-15
 */
@Service
public class RxRegistrationExamineServiceImpl extends ServiceImpl<RxRegistrationExamineMapper, RxRegistrationExamine> implements RxRegistrationExamineService {
    @Resource
    private RxRegistrationAdvanceStudentInfoMapper rxRegistrationAdvanceStudentInfoMapper;
    @Resource
    private RxRegistrationExamineBlackListMapper rxRegistrationExamineBlackListMapper;
    @Resource
    private RxRegistrationPlanSettingMapper rxRegistrationPlanSettingMapper;
    @Resource
    private RxRegistrationPlanAppSettingMapper rxRegistrationPlanAppSettingMapper;

    @Override
    public ResponseResult registrationExamine(String stuIdCard) {
        RxRegistrationExamineResp examineResp = new RxRegistrationExamineResp();

        boolean validIDCard = ParamCheckUtil.isValidIDCard(stuIdCard);
        if (!validIDCard) {
            return CommonResult.failed(CommonCodeEnum.ENROLLMENT_MANAGEMENT_STUDENT_ID_CARD_ERR);
        }

        // 1.判断是否在黑名单中
        List<RxRegistrationExamineBlackList> blackList = rxRegistrationExamineBlackListMapper.selectList(new LambdaQueryWrapper<RxRegistrationExamineBlackList>()
                .eq(RxRegistrationExamineBlackList::getStuIdCard, stuIdCard));
        if (blackList != null && blackList.size() > 0) {
            examineResp.setBlacklist(true);
            return CommonResult.success(examineResp);
        }
        examineResp.setBlacklist(false);

        // 2.判断招生计划是否开启
        List<RxRegistrationPlanSetting> planSettingList = rxRegistrationPlanSettingMapper.selectList(new LambdaQueryWrapper<RxRegistrationPlanSetting>()
                .eq(RxRegistrationPlanSetting::getPlanStatus, PLAN_STATUS_ENABLE));
        if (planSettingList == null || planSettingList.size() == 0) {
            examineResp.setOpenPlan(false);
            return CommonResult.success(examineResp);
        }
        examineResp.setOpenPlan(true);
        RxRegistrationPlanSetting planSetting = planSettingList.get(0);

        // 3.判断学生招生阶段
//        List<RxRegistrationAdvanceStudentInfo> studentInfoList = rxRegistrationAdvanceStudentInfoMapper.selectList(new LambdaQueryWrapper<RxRegistrationAdvanceStudentInfo>()
//                .eq(RxRegistrationAdvanceStudentInfo::getIdCard, examineReq.getStuIdCard())
//                .eq(RxRegistrationAdvanceStudentInfo::getPlanId, planSetting.getId()));

        //获取招生计划下阶段招生开放时间
        Date nowDate = new Date();
        List<RxRegistrationPlanAppSetting> planAppSettingList = rxRegistrationPlanAppSettingMapper.selectList(new LambdaQueryWrapper<RxRegistrationPlanAppSetting>()
                .eq(RxRegistrationPlanAppSetting::getPlanId, planSetting.getId())
                .lt(RxRegistrationPlanAppSetting::getBgTime, nowDate) //开始时间小于当前时间
                .gt(RxRegistrationPlanAppSetting::getEndTime, nowDate)  //结束时间大于当前时间
                .orderByDesc(RxRegistrationPlanAppSetting::getAppType));
        if (planAppSettingList == null || planAppSettingList.size() == 0) {
            examineResp.setOpenStep(false);
            return CommonResult.success(examineResp);
        }
        examineResp.setOpenStep(true);

        examineResp.setStuRegistrationStage(planAppSettingList.get(0).getAppType());
        return CommonResult.success(examineResp);
    }
}
